﻿using ITSM.Model;
using ITSM.Model.Entity;
using ITSM.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ITSM.Service
{
    public class DemandOperate : BaseDB, IDemandOperate
    {
        private readonly IDemand _demand;
        private readonly IWxMessage _wxMessage;
        private readonly ICompany _company;
        private readonly IDemandType _demandType;

        public DemandOperate(IDemand demand, IWxMessage wxMessage, ICompany company, IDemandType demandType)
        {
            _demand = demand;
            _wxMessage = wxMessage;
            _company = company;
            _demandType = demandType;
        }

        public bool ReceivingOrders(M_DemandOperate demandOperate)
        {
            var demand = db.Queryable<RYD_DemandList>().Where(res => res.RYD_DemandId == demandOperate.RYD_DemandId && res.RYD_State == 1).First();
            if (demand == null)
            {
                return false;
            }
            if (demand.RYD_ItId == demandOperate.RYD_UserId || demand.RYD_ToItId == demandOperate.RYD_UserId)
            {
                demand.RYD_State = 2;
                demand.RYD_ItStartTime = DateTime.Now;
                demand.RYD_ModifyBy = demandOperate.RYD_UserId;
                demand.RYD_ModifyTime = DateTime.Now;

                if (db.Updateable(demand).ExecuteCommand() > 0)
                {
                    new QuartzNetJob.QuartzHelper().RemoveJob(demandOperate.RYD_DemandId + "");
                    var userInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == demand.RYD_UserId).First();
                    var itUserInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == (demand.RYD_ToItId ?? demand.RYD_ItId)).First();
                    string urlStart = AppHelper.ReadAppSettings("wxUrlStart");
                    string urlEnd = AppHelper.ReadAppSettings("wxUrlEnd");
                    string typeName = db.Queryable<RYD_DemandType>().Where(res => res.RYD_DemandTypeNo == demand.RYD_TypeId).First().RYD_DemandTypeName;
                    string levelName = db.Queryable<RYD_FieldDict>().Where(res => res.RYD_FieldName == "DemandTypeLevel" && res.RYD_FieldValue == demand.RYD_Level.ToString()).First().RYD_FieldText;
                    M_Template template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = userInfo.RYD_WxNo,
                        url = urlStart + "RYD_DemandId=" + demand.RYD_DemandId + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "接单成功提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "接单时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = "IT人员已接单，请等待处理！"
                            }
                        }
                    };
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    _wxMessage.SendMessage(template);
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = itUserInfo.RYD_WxNo,
                        url = urlStart + "RYD_DemandId=" + demand.RYD_DemandId + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "接单成功提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "接单时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = "你已接单，请尽快处理！ "
                            }
                        }
                    };
                    _wxMessage.SendMessage(template);

                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
            throw new NotImplementedException();
        }

        public bool CompletionOrder(M_DemandOperate demandOperate)
        {
            var demand = db.Queryable<RYD_DemandList>().Where(res => res.RYD_DemandId == demandOperate.RYD_DemandId && res.RYD_State == 2).First();
            if (demand == null)
            {
                return false;
            }
            if (demand.RYD_ItId == demandOperate.RYD_UserId || demand.RYD_ToItId == demandOperate.RYD_UserId)
            {
                demand.RYD_State = 3;
                demand.RYD_ItEndTime = DateTime.Now;
                demand.RYD_Reason = demandOperate.RYD_Reason;
                demand.RYD_Solution = demandOperate.RYD_Solution;
                demand.RYD_KBId = demandOperate.RYD_KBId;
                demand.RYD_ModifyBy = demandOperate.RYD_UserId;
                demand.RYD_ModifyTime = DateTime.Now;
                if (db.Updateable(demand).ExecuteCommand() > 0)
                {
                    var userInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == demand.RYD_UserId).First();
                    var itUserInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == (demand.RYD_ToItId ?? demand.RYD_ItId)).First();
                    string urlStart = AppHelper.ReadAppSettings("wxUrlStart");
                    string urlEnd = AppHelper.ReadAppSettings("wxUrlEnd");
                    string typeName = db.Queryable<RYD_DemandType>().Where(res => res.RYD_DemandTypeNo == demand.RYD_TypeId).First().RYD_DemandTypeName;
                    string levelName = db.Queryable<RYD_FieldDict>().Where(res => res.RYD_FieldName == "DemandTypeLevel" && res.RYD_FieldValue == demand.RYD_Level.ToString()).First().RYD_FieldText;
                    M_Template template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = userInfo.RYD_WxNo,
                        url = urlStart + "RYD_DemandId=" + demand.RYD_DemandId + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "订单完成提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "完单时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName +
                                                "\n问题原因:\t" + demand.RYD_Reason +
                                                "\n解决方案:\t" + demand.RYD_Solution
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = "订单处理完成，请查看问题原因和解决方案！"
                            }
                        }
                    };
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    _wxMessage.SendMessage(template);
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = itUserInfo.RYD_WxNo,
                        url = urlStart + "RYD_DemandId=" + demand.RYD_DemandId + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "完单成功提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "完单时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName +
                                                "\n问题原因:\t" + demand.RYD_Reason +
                                                "\n解决方案:\t" + demand.RYD_Solution
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = "感谢你的帮助，谢谢！ "
                            }
                        }
                    };
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    _wxMessage.SendMessage(template);
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
            throw new NotImplementedException();
        }

        public List<M_TransferOrdersUsers> GetTransferOrdersUsers(string RYD_Company)
        {
            try
            {
                List<M_TransferOrdersUsers> listM = new();
                var listType = db.Queryable<RYD_DemandType, RYD_DemandTypeLink, RYD_UserInfo>((type, link, us) => type.RYD_DemandTypeNo == link.RYD_DemandTypeNo && us.RYD_Id == link.RYD_UserId).Where((type, link, us) => type.RYD_Company == RYD_Company && type.RYD_IsDelete == 1 && us.RYD_Company == RYD_Company).GroupBy((type, link, us) => new { type.RYD_Id, type.RYD_DemandTypeNo, type.RYD_DemandTypeName }).Select((type, link, us) => new RYD_DemandType { RYD_Id = type.RYD_Id, RYD_DemandTypeNo = type.RYD_DemandTypeNo, RYD_DemandTypeName = type.RYD_DemandTypeName }).ToList();

                foreach (var item in listType)
                {
                    M_TransferOrdersUsers itemM = new M_TransferOrdersUsers()
                    {
                        RYD_TypeNo = item.RYD_DemandTypeNo,
                        RYD_TypeName = item.RYD_DemandTypeName,
                    };
                    var list = db.Queryable<RYD_DemandTypeLink, RYD_UserInfo>((res, us) => res.RYD_UserId == us.RYD_Id).Where((res, us) => res.RYD_DemandTypeNo == item.RYD_DemandTypeNo && (res.RYD_Company == us.RYD_Company||us.AcrossCompanies==1) && us.RYD_IsDelete == 1 && (us.RYD_State == 2 || (us.RYD_State == 4 && !string.IsNullOrWhiteSpace(us.RYD_Name)))).Select((res, us) => res).ToList();
                    var userList = _demand.GetUserState(list);
                    userList ??= new();
                    List<M_TransferOrdersUsers.ChildrenC> listC = new();
                    listC = userList.Select(res => new M_TransferOrdersUsers.ChildrenC() { Sum = res.Sum, Level = res.Level, Num = res.Num, RYD_UserId = res.RYD_UserId, RYD_UserName = res.RYD_UserName }).ToList();
                    itemM.Children = listC;
                    listM.Add(itemM);
                }

                return listM;
            }
            catch (Exception)
            {
                return new List<M_TransferOrdersUsers>();
            }
        }

        public bool TransferOrders(M_DemandOperate demandOperate)
        {
            var demand = db.Queryable<RYD_DemandList>().Where(res => res.RYD_DemandId == demandOperate.RYD_DemandId && res.RYD_State == 1).First();
            if (demand == null)
            {
                return false;
            }
            if (demand.RYD_ItId == demandOperate.RYD_UserId || demand.RYD_ToItId == demandOperate.RYD_UserId)
            {
                string isTo = "";
                if (demand.RYD_ToItId != null)
                {
                    isTo = demand.RYD_ToItId;
                }
                demand.RYD_TypeId = demandOperate.RYD_ToType;
                demand.RYD_ToItId = demandOperate.RYD_ToUserId;
                demand.RYD_ModifyBy = demandOperate.RYD_ToUserId;
                demand.RYD_ModifyTime = DateTime.Now;

                if (db.Updateable(demand).ExecuteCommand() > 0)
                {
                    new QuartzNetJob.QuartzHelper().RemoveJob(demandOperate.RYD_DemandId + "");
                    var userInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == demand.RYD_UserId).First();
                    string itid = demand.RYD_ItId;
                    if (!string.IsNullOrWhiteSpace(isTo))
                    {
                        itid = isTo;
                    }
                    var itUserInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == itid).First();
                    var toItUserInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == demand.RYD_ToItId).First();
                    string urlStart = AppHelper.ReadAppSettings("wxUrlStart");
                    string urlEnd = AppHelper.ReadAppSettings("wxUrlEnd");
                    string typeName = db.Queryable<RYD_DemandType>().Where(res => res.RYD_DemandTypeNo == demand.RYD_TypeId).First().RYD_DemandTypeName;
                    string levelName = db.Queryable<RYD_FieldDict>().Where(res => res.RYD_FieldName == "DemandTypeLevel" && res.RYD_FieldValue == demand.RYD_Level.ToString()).First().RYD_FieldText;

                    #region 向用户发送消息提醒
                    M_Template template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = userInfo.RYD_WxNo,
                        url = urlStart + "RYD_DemandId=" + demand.RYD_DemandId + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "订单转单提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "转单时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\n原IT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n现IT负责人:\t" + toItUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = "订单负责人发生变化，请查看！"
                            }
                        }
                    };
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    _wxMessage.SendMessage(template);
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法


                    #endregion

                    #region 向原IT负责人发送消息提醒
                    template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = itUserInfo.RYD_WxNo,
                        url = urlStart + "RYD_DemandId=" + demand.RYD_DemandId + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "转单成功提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "转单时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\n原IT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n现IT负责人:\t" + toItUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = "你已转单！ "
                            }
                        }
                    };
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    _wxMessage.SendMessage(template);
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法

                    #endregion

                    #region 向新IT负责人发送消息提醒
                    template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = toItUserInfo.RYD_WxNo,
                        url = urlStart + "RYD_DemandId=" + demand.RYD_DemandId + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "转单成功提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "转单时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\n原IT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n现IT负责人:\t" + toItUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = "已将订单转移给你，请查看！ "
                            }
                        }
                    };
                    _wxMessage.SendMessage(template);

                    QuartzNetJob.QuartzHelper q = new();
                    int minutes = 15;
                    var levelMinutes = db.Queryable<RYD_FieldDict>().Where(res => res.RYD_FieldName == "DemandTypeLevel" && res.RYD_FieldValue == demand.RYD_Level.ToString() && res.RYD_State == 1).ToList().FirstOrDefault();
                    if (levelMinutes != null)
                    {
                        minutes = Convert.ToInt32(levelMinutes.RYD_Remark);
                    }
                    q.SetIntervalJob<JobWork.JobSendMessage>(minutes, template, demand.RYD_DemandId + "");

                    #endregion


                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
            throw new NotImplementedException();
        }

        public bool CancelOrder(M_DemandOperate demandOperate)
        {
            var demand = db.Queryable<RYD_DemandList>().Where(res => res.RYD_DemandId == demandOperate.RYD_DemandId && res.RYD_State == 1).First();
            if (demand == null)
            {
                return false;
            }
            if (demand.RYD_UserId == demandOperate.RYD_UserId)
            {
                demand.RYD_State = -2;
                demand.RYD_ModifyBy = demandOperate.RYD_UserId;
                demand.RYD_ModifyTime = DateTime.Now;

                if (db.Updateable(demand).ExecuteCommand() > 0)
                {
                    new QuartzNetJob.QuartzHelper().RemoveJob(demandOperate.RYD_DemandId + "");
                    var userInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == demand.RYD_UserId).First();
                    var itUserInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == (demand.RYD_ToItId ?? demand.RYD_ItId)).First();
                    string urlStart = AppHelper.ReadAppSettings("wxUrlStart");
                    string urlEnd = AppHelper.ReadAppSettings("wxUrlEnd");
                    string typeName = db.Queryable<RYD_DemandType>().Where(res => res.RYD_DemandTypeNo == demand.RYD_TypeId).First().RYD_DemandTypeName;
                    string levelName = db.Queryable<RYD_FieldDict>().Where(res => res.RYD_FieldName == "DemandTypeLevel" && res.RYD_FieldValue == demand.RYD_Level.ToString()).First().RYD_FieldText;
                    M_Template template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = userInfo.RYD_WxNo,
                        url = urlStart + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "订单撤销提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "撤单时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = "订单已撤销！请前往暂存查看"
                            }
                        }
                    };
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    _wxMessage.SendMessage(template);
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = itUserInfo.RYD_WxNo,
                        url = urlStart + "RYD_DemandId=" + demand.RYD_DemandId + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "订单撤销提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "撤单时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = "用户已撤销该订单，请注意！ "
                            }
                        }
                    };
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    _wxMessage.SendMessage(template);
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
            throw new NotImplementedException();
        }

        public bool EvaluationOrder(M_DemandOperate demandOperate)
        {
            var demand = db.Queryable<RYD_DemandList>().Where(res => res.RYD_DemandId == demandOperate.RYD_DemandId && res.RYD_State == 3).First();
            if (demand == null)
            {
                return false;
            }
            if (demand.RYD_UserId == demandOperate.RYD_UserId)
            {
                demand.RYD_State = 4;
                demand.RYD_UserEndTime = DateTime.Now;
                demand.RYD_Star = demandOperate.RYD_Star;
                demand.RYD_Evaluate = demandOperate.RYD_Evaluate;
                demand.RYD_ModifyBy = demandOperate.RYD_UserId;
                demand.RYD_ModifyTime = DateTime.Now;
                if (db.Updateable(demand).ExecuteCommand() > 0)
                {
                    var userInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == demand.RYD_UserId).First();
                    var itUserInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == (demand.RYD_ToItId ?? demand.RYD_ItId)).First();
                    string urlStart = AppHelper.ReadAppSettings("wxUrlStart");
                    string urlEnd = AppHelper.ReadAppSettings("wxUrlEnd");
                    string typeName = db.Queryable<RYD_DemandType>().Where(res => res.RYD_DemandTypeNo == demand.RYD_TypeId).First().RYD_DemandTypeName;
                    string levelName = db.Queryable<RYD_FieldDict>().Where(res => res.RYD_FieldName == "DemandTypeLevel" && res.RYD_FieldValue == demand.RYD_Level.ToString()).First().RYD_FieldText;
                    M_Template template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = userInfo.RYD_WxNo,
                        url = urlStart + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "订单评价完成提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "评价时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName +
                                                "\n评价星级:\t" + demand.RYD_Star +
                                                "\n评价内容:\t" + demand.RYD_Evaluate
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = "感谢你对本次服务的评价！"
                            }
                        }
                    };
                    _wxMessage.SendMessage(template);
                    template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = itUserInfo.RYD_WxNo,
                        url = urlStart + "RYD_DemandId=" + demand.RYD_DemandId + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "订单评价完成提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "评价时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName +
                                                "\n评价星级:\t" + demand.RYD_Star +
                                                "\n评价内容:\t" + demand.RYD_Evaluate
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = "用户已对本次服务进行评价，请查看！ "
                            }
                        }
                    };
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    _wxMessage.SendMessage(template);
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
            throw new NotImplementedException();
        }

        public bool CompanyTransferOrders(M_DemandOperate demandOperate)
        {
            var demand = db.Queryable<RYD_DemandList>().Where(res => res.RYD_DemandId == demandOperate.RYD_DemandId && res.RYD_State == 1).First();
            if (demand == null)
            {
                return false;
            }
            if (demand.RYD_ItId == demandOperate.RYD_UserId || demand.RYD_ToItId == demandOperate.RYD_UserId)
            {
                string isTo = "";
                if (demand.RYD_ToItId != null)
                {
                    isTo = demand.RYD_ToItId;
                }
                demand.RYD_Company = demandOperate.RYD_ToCompany;
                demand.RYD_TypeId = demandOperate.RYD_ToType;
                demand.RYD_ToItId = demandOperate.RYD_ToUserId;
                demand.RYD_ModifyBy = demandOperate.RYD_ToUserId;
                demand.RYD_ModifyTime = DateTime.Now;

                if (db.Updateable(demand).ExecuteCommand() > 0)
                {
                    new QuartzNetJob.QuartzHelper().RemoveJob(demandOperate.RYD_DemandId + "");
                    var userInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == demand.RYD_UserId).First();
                    string itid = demand.RYD_ItId;
                    if (!string.IsNullOrWhiteSpace(isTo))
                    {
                        itid = isTo;
                    }
                    var itUserInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == itid).First();
                    var toItUserInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == demand.RYD_ToItId).First();
                    string urlStart = AppHelper.ReadAppSettings("wxUrlStart");
                    string urlEnd = AppHelper.ReadAppSettings("wxUrlEnd");
                    string typeName = db.Queryable<RYD_DemandType>().Where(res => res.RYD_DemandTypeNo == demand.RYD_TypeId).First().RYD_DemandTypeName;
                    string levelName = db.Queryable<RYD_FieldDict>().Where(res => res.RYD_FieldName == "DemandTypeLevel" && res.RYD_FieldValue == demand.RYD_Level.ToString()).First().RYD_FieldText;
                    string RYD_Company = db.Queryable<RYD_FieldDict>().Where(res => res.RYD_FieldName == "Company" && res.RYD_FieldValue == demand.RYD_Company.ToString()).First().RYD_FieldText;
                    M_Template template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = userInfo.RYD_WxNo,
                        url = urlStart + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "订单转移提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "转移时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = $"你的订单将转移至 {RYD_Company}！"
                            }
                        }
                    };
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    _wxMessage.SendMessage(template);
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = itUserInfo.RYD_WxNo,
                        url = urlStart + "RYD_DemandId=" + demand.RYD_DemandId + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "订单转移提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "转移时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = $"你已将订单转移至 {RYD_Company}！ "
                            }
                        }
                    };
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    _wxMessage.SendMessage(template);
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = toItUserInfo.RYD_WxNo,
                        url = urlStart + "RYD_DemandId=" + demand.RYD_DemandId + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "订单转移提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "转移时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\n原IT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n现IT负责人:\t" + toItUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = $"有订单转移至 {RYD_Company}，请你尽快处理！ "
                            }
                        }
                    };
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    _wxMessage.SendMessage(template);
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法

                    QuartzNetJob.QuartzHelper q = new();
                    int minutes = 15;
                    var levelMinutes = db.Queryable<RYD_FieldDict>().Where(res => res.RYD_FieldName == "DemandTypeLevel" && res.RYD_FieldValue == demand.RYD_Level.ToString() && res.RYD_State == 1).ToList().FirstOrDefault();
                    if (levelMinutes != null)
                    {
                        minutes = Convert.ToInt32(levelMinutes.RYD_Remark);
                    }
                    q.SetIntervalJob<JobWork.JobSendMessage>(minutes, template, demand.RYD_DemandId + "");

                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
            throw new NotImplementedException();
        }

        public bool FallbackOrder(M_DemandOperate demandOperate)
        {
            var demand = db.Queryable<RYD_DemandList>().Where(res => res.RYD_DemandId == demandOperate.RYD_DemandId && res.RYD_State == 1).First();
            if (demand == null)
            {
                return false;
            }
            if ((demand.RYD_ItId == demandOperate.RYD_UserId && demand.RYD_ToItId == null) || demand.RYD_ToItId == demandOperate.RYD_UserId)
            {
                demand.RYD_State = -2;
                demand.RYD_Fallback = demandOperate.RYD_Fallback;
                demand.RYD_ModifyBy = demandOperate.RYD_UserId;
                demand.RYD_ModifyTime = DateTime.Now;

                if (db.Updateable(demand).ExecuteCommand() > 0)
                {
                    new QuartzNetJob.QuartzHelper().RemoveJob(demandOperate.RYD_DemandId + "");
                    var userInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == demand.RYD_UserId).First();
                    var itUserInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == (demand.RYD_ToItId ?? demand.RYD_ItId)).First();
                    string urlStart = AppHelper.ReadAppSettings("wxUrlStart");
                    string urlEnd = AppHelper.ReadAppSettings("wxUrlEnd");
                    string typeName = db.Queryable<RYD_DemandType>().Where(res => res.RYD_DemandTypeNo == demand.RYD_TypeId).First().RYD_DemandTypeName;
                    string levelName = db.Queryable<RYD_FieldDict>().Where(res => res.RYD_FieldName == "DemandTypeLevel" && res.RYD_FieldValue == demand.RYD_Level.ToString()).First().RYD_FieldText;
                    M_Template template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = userInfo.RYD_WxNo,
                        url = urlStart + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "订单回退提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "退回时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName +
                                                "\n回退原因:\t" + demand.RYD_Fallback
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = "IT负责人已将订单退回，请查看回退原因！"
                            }
                        }
                    };
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    _wxMessage.SendMessage(template);
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    template = new M_Template()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = itUserInfo.RYD_WxNo,
                        url = urlStart + "RYD_DemandId=" + demand.RYD_DemandId + urlEnd,
                        data = new M_Template.Data()
                        {
                            first = new M_Template.valueColor()
                            {
                                value = "订单回退提醒！"
                            },
                            keyword1 = new M_Template.valueColor()
                            {
                                value = "退回时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_Template.valueColor()
                            {
                                value = "订单单号:\t" + demand.RYD_DemandId +
                                                "\n类别:\t" + typeName +
                                                "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                                "\n申请人:\t" + demand.RYD_UserName +
                                                "\n事件等级:\t" + levelName +
                                                "\n回退原因:\t" + demand.RYD_Fallback
                            },
                            remark = new M_Template.valueColor()
                            {
                                value = "已退回该订单！ "
                            }
                        }
                    };
#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    _wxMessage.SendMessage(template);
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
            throw new NotImplementedException();
        }

        public bool DeleteOrder(M_DemandOperate demandOperate)
        {
            var demand = db.Queryable<RYD_DemandList>().Where(res => res.RYD_DemandId == demandOperate.RYD_DemandId && res.RYD_State == -2).First();
            if (demand == null)
            {
                return false;
            }
            if (demand.RYD_UserId == demandOperate.RYD_UserId)
            {
                demand.RYD_State = -1;
                demand.RYD_ModifyBy = demandOperate.RYD_UserId;
                demand.RYD_ModifyTime = DateTime.Now;
                int retInt = db.Updateable(demand).ExecuteCommand();
                return retInt > 0;
            }
            return false;
            throw new NotImplementedException();
        }

        public List<M_TransferOrdersUsers> GetCompanyTransferOrdersUsers(string RYD_Company)
        {
            try
            {
                List<M_TransferOrdersUsers> listM = new();
                var listCompany = _company.QueryCompany();
                foreach (var item in listCompany)
                {
                    M_TransferOrdersUsers itemM = new()
                    {
                        RYD_CompanyName = item.RYD_CompanyName,
                        RYD_CompanyNo = item.RYD_CompanyNo,
                    };
                    var list = _demandType.QueryDemandType(item.RYD_CompanyNo);

                    list ??= new();
                    List<M_TransferOrdersUsers.ChildrenT> listT1 = new();
                    listT1 = list.Select(res => new M_TransferOrdersUsers.ChildrenT() { RYD_TypeName = res.RYD_DemandTypeName, RYD_TypeNo = res.RYD_DemandTypeNo }).ToList();
                    itemM.ChildrenT1 = listT1;
                    listM.Add(itemM);
                }
                return listM;
            }
            catch (Exception)
            {
                return new List<M_TransferOrdersUsers>();
            }
        }

        public bool UpdateTypeOrder(M_DemandOperate demandOperate)
        {
            bool flag = false;
            if (db.Updateable<RYD_DemandList>()
                .SetColumns(it => new RYD_DemandList() { RYD_TypeId = demandOperate.RYD_ToType, RYD_ModifyBy = demandOperate.RYD_UserId, RYD_ModifyTime = DateTime.Now })
                .Where(res => res.RYD_DemandId == demandOperate.RYD_DemandId && res.RYD_Company == res.RYD_Company).ExecuteCommand() > 0)
            {
                flag = true;
                var demandlist = db.Queryable<RYD_DemandList>().Where(res => res.RYD_DemandId == demandOperate.RYD_DemandId && res.RYD_Company == res.RYD_Company).First();
                var userInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == demandlist.RYD_UserId).First();
                var itUserInfo = db.Queryable<RYD_UserInfo>().Where(res => res.RYD_Id == (demandlist.RYD_ToItId ?? demandlist.RYD_ItId)).First();
                string urlStart = AppHelper.ReadAppSettings("wxUrlStart");
                string urlEnd = AppHelper.ReadAppSettings("wxUrlEnd");
                string typeName = db.Queryable<RYD_DemandType>().Where(res => res.RYD_DemandTypeNo == demandlist.RYD_TypeId).First().RYD_DemandTypeName;
                string levelName = db.Queryable<RYD_FieldDict>().Where(res => res.RYD_FieldName == "DemandTypeLevel" && res.RYD_FieldValue == demandlist.RYD_Level.ToString()).First().RYD_FieldText;
                M_Template template = new()
                {
                    client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                    touser = userInfo.RYD_WxNo,
                    url = urlStart + urlEnd,
                    data = new M_Template.Data()
                    {
                        first = new M_Template.valueColor()
                        {
                            value = "订单类别修改提醒！"
                        },
                        keyword1 = new M_Template.valueColor()
                        {
                            value = "修改时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                        },
                        keyword2 = new M_Template.valueColor()
                        {
                            value = "订单单号:\t" + demandlist.RYD_DemandId +
                                            "\n类别:\t" + typeName +
                                            "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                            "\n申请人:\t" + demandlist.RYD_UserName +
                                            "\n事件等级:\t" + levelName
                        },
                        remark = new M_Template.valueColor()
                        {
                            value = "IT负责人已重新评估需求类别，请注意！"
                        }
                    }
                };
                _wxMessage.SendMessage(template);
                template = new M_Template()
                {
                    client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                    touser = itUserInfo.RYD_WxNo,
                    url = urlStart + "RYD_DemandId=" + demandlist.RYD_DemandId + urlEnd,
                    data = new M_Template.Data()
                    {
                        first = new M_Template.valueColor()
                        {
                            value = "订单类别修改提醒！"
                        },
                        keyword1 = new M_Template.valueColor()
                        {
                            value = "修改时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")
                        },
                        keyword2 = new M_Template.valueColor()
                        {
                            value = "订单单号:\t" + demandlist.RYD_DemandId +
                                            "\n类别:\t" + typeName +
                                            "\nIT负责人:\t" + itUserInfo.RYD_Name +
                                            "\n申请人:\t" + demandlist.RYD_UserName +
                                            "\n事件等级:\t" + levelName
                        },
                        remark = new M_Template.valueColor()
                        {
                            value = "已重新修改订单类别！ "
                        }
                    }
                };
                _wxMessage.SendMessage(template);
            }
            return flag;
        }
    }
}
